<template>
	<view class="container">
		<view >
			<hr class="chr" />
			<view class="rowborder">
				
				<!-- <picker mode="date" :value="query.ddate" @change="bindDateChange"  class="topborder">
						<text class="itemth">日期:</text><text class="inputborder">{{query.ddate}}</text>
					</picker> -->
				<!-- #ifdef H5 -->
			
				<!-- #endif -->
				<view>
					<button style="margin-left: 20rpx;" size="mini" type="default" @click="openshowProduceOrder">质检>>收料</button>
					<button style="margin-left: 20rpx;" size="mini" type="default" @click="openshowJieshou">收料>>质检</button>
				</view>
				<view class="billflex">
					<uni-easyinput style="width: 120upx;" placeholder="需求单号" v-model="query.csocode" class="inputbill"></uni-easyinput>
					<uni-easyinput style="width: 120upx;" placeholder="生产单号" v-model="query.mocode" class="inputbill"></uni-easyinput>
					<button size="mini" type="default" @click="findAll">查询</button>
				</view>
				
			</view>
			<hr class="chr"/>
		</view>
		
		<view style="overflow-y: scroll; max-height: 95vh" >
			<view v-for="(item, index) in datas" v-bind:key="index" >
				<navigator :url="'../rpproduce/index?id=' + item.id" class="navborder">
					<view style="color: #ff0000;float: right;margin: 20upx;">{{ item.measurement }}</view>
					<view>
						<text class="itemth">需求跟踪号:</text>
						<text class="itmemain">{{ item.csocode }}</text>
					</view>
					<view style="display: flex;">
						<view style="width: 300px;">
							<text class="itemth">生产单号:</text>
							<text class="itmemain">{{ item.mocode }}</text>
						</view>
						<view>
							<text class="itemth">明细单号:</text>
							<text class="itmemain">{{ item.modid }}</text>
						</view>
					</view>
					<view style="display: flex;">
						<view style="width: 300px;">
							<text class="itemth">部门名称:</text>
							<text class="itmemain">{{ item.cdepname }}</text>
						</view>
						<view>
							<text class="itemth">楼层:</text>
							<text class="itmemain">{{ item.define33 }}</text>
						</view>
					</view>
					<view style="display: flex;">
						<view style="width: 300px;">
							<text class="itemth">产品名称:</text>
							<text class="itmemain">{{ item.cinvname }}</text>
						</view>
						<view>
							<text class="itemth">需求跟踪号:</text>
							<text class="itmemain">{{ item.csocode }}</text>
						</view>
					</view>
					<view style="display: flex;">
						<view style="width: 300px;">
							<text class="itemth">数量:</text>
							<text class="itmemain">{{ item.qty }}</text>
						</view>
						<view>
							<text class="itemth">日期:</text>
							<text class="itmemain">{{ item.createTime }}</text>
						</view>
					</view>
				</navigator>
			</view>
		</view>
		<uni-popup ref="showProduceOrder" type="center" :animation="false" :mask-click="false">
			<view style="background-color: #FFFFFF;width: 600upx;height: 250upx;padding: 40upx;margin-top: 60upx;">
				<div class="rowborder">
					<div class="billflex">
						<button size="mini" type="default" @click="closeshowProduceOrder">取消</button>
						<uni-easyinput style="width: 120upx;" placeholder="生产单号" v-model="mocode" class="inputbill"></uni-easyinput>
						<uni-easyinput style="width: 120upx;" placeholder="销售单号" v-model="csocode" class="inputbill"></uni-easyinput>
						<button size="mini" type="default" @click="findProd">查询</button>
						<div><uni-icons style="margin-left: 50rpx;" type="scan" size="30" @click="scan()"></uni-icons></div>
						<!-- #ifdef H5 -->
							<uni-easyinput style="width: 120upx;" placeholder="二维码" v-model="cbarcode" class="inputbill"></uni-easyinput>
							<button size="mini" type="default" @click="h5input()">二维码查询</button>
						<!-- #endif -->
					</div>
				</div>
				<view>
					<scroll-view scroll-y="true" class="scroll-Y">
						<uni-list>
							<uni-list-item v-for="(item, index) in prodDatas" v-bind:key="index" style="height: 100px;" showArrow="true">
								<view slot="body" @click="selProd(index)">
									<view style="display: flex;">
										<view style="width: 200upx;">
											<text>生产单号：{{ item.MoCode }}-{{ item.SortSeq }}</text>
										</view>
										<view style="width: 200upx;">
											<text>销售单号：{{ item.SoCode }}-{{ item.SoSeq }}</text>
										</view>
									</view>
									<view style="display: flex;">
										<view style="width: 200upx;">
											<text>数量：{{ item.Qty }}</text>
										</view>
										<view >
											<text>产品名称：{{ item.cInvName }}</text>
										</view>
									</view>
								</view>
							</uni-list-item>
						</uni-list>
					</scroll-view>
				</view>
			</view>
		</uni-popup>
		
		<uni-popup ref="showJieshou" type="center" :animation="false" :mask-click="false">
			<view style="background-color: #FFFFFF;width: 600upx;height: 250upx;padding: 40upx;margin-top: 60upx;">
				<div class="rowborder">
					<div class="billflex">
						<button size="mini" type="default" @click="closeshowJieshou()">取消</button>
						<uni-easyinput style="width: 120upx;" placeholder="生产单号" v-model="mocode" class="inputbill"></uni-easyinput>
						<uni-easyinput style="width: 120upx;" placeholder="销售单号" v-model="csocode" class="inputbill"></uni-easyinput>
						<button size="mini" type="default" @click="findJieshou">查询</button>
						<div><uni-icons style="margin-left: 50rpx;" type="scan" size="30" @click="scan()"></uni-icons></div>
					<!-- #ifdef H5 -->
						<uni-easyinput style="width: 120upx;" placeholder="二维码" v-model="cbarcode" class="inputbill"></uni-easyinput>
						<button size="mini" type="default" @click="h5input()">二维码查询</button>
					<!-- #endif -->
					</div>
				</div>
				<view>
					<scroll-view scroll-y="true" class="scroll-Y">
						<uni-list>
							<uni-list-item v-for="(item, index) in jieshouDatas" v-bind:key="index" style="height: 100px;" showArrow="true">
								<view slot="body" @click="selJieshou(index)">
									<view style="display: flex;">
										<view style="width: 200upx;">
											<text>生产单号：{{ item.MoCode }}-{{ item.SortSeq }}</text>
										</view>
										<view style="width: 200upx;">
											<text>销售单号：{{ item.SoCode }}-{{ item.SoSeq }}</text>
										</view>
									</view>
									<view style="display: flex;">
										<view style="width: 200upx;">
											<text>数量：{{ item.Qty }}</text>
										</view>
										<view >
											<text>产品名称：{{ item.cInvName }}</text>
										</view>
									</view>
								</view>
							</uni-list-item>
						</uni-list>
					</scroll-view>
				</view>
			</view>
		</uni-popup>
		
		<uni-popup ref="showPPbarcode" type="center" :animation="false" :mask-click="false">
			<view style="background-color: #FFFFFF;width: 600upx;height: 250upx;padding: 40upx;margin-top: 60upx;">
				<view class="rowborder">
					<view>
						<button size="mini" type="default" @click="closeshowPPbarcode">取消</button>
						<button size="mini" type="default" @click="selBarcodes">确定</button>
						<button size="mini" type="default" @click="selcheck">全选</button>
					</view>
				</view>
				<view>
					<scroll-view scroll-y="true" class="scroll-Y">
						<uni-list>
							
							<uni-list-item v-for="(item, index) in barcodeDatas" v-bind:key="index" :showSwitch="!item.qctime&&!item.cstatus && ((qctype == '生产' && !item.slddate)||(item.slddate && qctype == '接收'))" @switchChange="item.checked = !item.checked" :switchChecked="item.checked">
								<view slot="body">
									<view style="display: flex;">
										<view style="width: 200upx;">
											<text>条码：{{ item.cbarcode }}--{{ item.cbarcodeno }}</text>
										</view>
										<view style="width: 50upx;">
											<text>{{ item.dissemblename }}</text>
										</view>
										<view style="display: flex;width: 180upx;">
											<view >
												<text>{{ item.slddate ? '已收':'待收' }}</text>
											</view>
											<text style="color: red;">|</text>
											<view>
												<text>{{ item.qctime ? '已检': item.stepvalue =='质检' ? '返工' :'待检' }}</text>
											</view>
											<view v-show="item.qctime|| item.stepvalue =='质检'" style="margin-left: 5rpx;">
												<button  size="mini" type="default" @click="showBill(item.cbarcode)">查看</button>
											</view>
											<view>
												<text style="font-size: 20rpx;color:#f10000;">{{ item.cstatus ? '作废':'' }}</text>
											</view>
										</view>
									</view>
								</view>
							</uni-list-item>
						</uni-list>
					</scroll-view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
export default {
	created() {
		//this.query.ddate = this.getNowFormatTime();
		this.findAll();
	},
	data() {
		return {
			datas: [],
			date: '',
			query: {
				pageIndex: 1,
				pageSize: 100,
				mocode: '',
				csocode:'',
				ddate: ''
			},
			cbarcode:'',
			qctype:'',
			csocode: '',
			mocode: '',
			modid:'',
			prodDatas: [],
			barcodeDatas:[],
			jieshouDatas:[]
		};
	},
	onShow(){
		if(this.modid){
			this.findPPbarcodeBymodid()
		}
	},
	methods: {
		findAll() {
			this.$http.post('rpProduce/query', this.query).then(res => {
				this.datas = res.data.data.page.content;
			});
		},
		selProd(ind){
			this.closeshowProduceOrder();
			this.modid = this.prodDatas[ind].MoDId
			this.findPPbarcodeBymodid()
		},
		selJieshou(ind){
			this.closeshowJieshou();
			this.modid = this.jieshouDatas[ind].MoDId
			this.findPPbarcodeBymodid()
		},
		findProd() {
			this.$http
				.get('selectMomorder/query?', {
					socode: this.csocode,
					mocode: this.mocode
				})
				.then(res => {
					this.prodDatas = res.data.data.page;
				});
		},
		selcheck(){
			//item.slddate 已接收
			//item.listNo == 0 待接收 
			//item.rds10id == 0 待接收 
			//item.qctime == null 待质检
			//statpvalue ==质检方式
			//cstatus :是否作废
			
			for(const item of this.barcodeDatas){
				if(!item.qctime&&!item.cstatus && 
				((this.qctype == '生产' && !item.slddate)||(item.slddate && this.qctype == '接收'))){
					item.checked = true
				}
			}
		},
		findJieshou() {
			this.$http
				.get('selectMomorder2/query?', {
					socode: this.csocode,
					mocode: this.mocode
				})
				.then(res => {
					this.jieshouDatas = res.data.data.page;
				});
		},
		selBarcodes(){
			let data = []
			let totalqty = 0;
			for(const item of this.barcodeDatas){
				if(item.checked){
					totalqty += item.qty
					data.push({cbarcode:item.cbarcode,cbarcodeno:item.cbarcodeno,qty:item.qty})
				}
			}
			if(data.length > 0){
				//this.closeshowPPbarcode()
				uni.navigateTo({
					url: '../rpproduce/index?modid=' 
					+ this.barcodeDatas[0].modid + 
					'&barcode=' + data[0].cbarcode + 
					'&qctype=' + this.qctype + 
					'&totalqty=' + totalqty + 
					'&barcodes=' + JSON.stringify(data)
				});
			}
		},
		findPPbarcodeBymodid() {
			this.$http
				.get('ppbarcode/findAllByModid?', {
					modid: this.modid
				})
				.then(res => {
					if(res.data.data.page){
						this.barcodeDatas = []
						for(const item of res.data.data.page){
							/* if(this.qctype == "接收" && item.rds10id == 0){
								continue;
							}
							if(this.qctype == "生产" && item.rds10id != 0){
								continue;
							} */
							item.checked = false;
							this.barcodeDatas.push(item)
						}
					}
					this.openshowPPbarcode();
				});
		},
		openshowProduceOrder() {
			this.qctype ="生产"
			this.$refs.showProduceOrder.open();
		},
		closeshowProduceOrder() {
			this.$refs.showProduceOrder.close();
		},
		openshowPPbarcode(){
			this.$refs.showPPbarcode.open();
		},
		closeshowPPbarcode(){
			this.$refs.showPPbarcode.close();
		},
		openshowJieshou(){
			this.qctype ="接收"
			this.$refs.showJieshou.open();
		},
		closeshowJieshou(){
			this.$refs.showJieshou.close();
		},
		dateSelect(e) {
			
			
		},
		bindDateChange(e) {
			this.query.ddate = e.detail.value;
			this.findAll();
		},
		getNowFormatTime() {
			let date = new Date();
			let seperator1 = '-';
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let strDate = date.getDate();
			if (month >= 1 && month <= 9) {
				month = '0' + month;
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = '0' + strDate;
			}
			let currentdate = year + seperator1 + month + seperator1 + strDate;
			return currentdate;
		},
		showBill(cbarcode){
			console.log(cbarcode)
			this.$http
				.get('rpProduce/findRpProduceBySubBarcode', {
					ppbarcode: cbarcode
				})
				.then(res => {
					if (res.data.data.data) {
						const item = res.data.data.data;
						uni.navigateTo({
							url: '../rpproduce/index?id=' + item.id
						});
					} else {
						uni.showToast({
							title: '条码不存在',
							icon: 'error'
						});
					}
				});
		},
		getBarcode(barcode) {
			this.$http
				.get('ppbarcode/findByBarcode', {
					barcode: barcode
				})
				.then(res => {
					if (res.data.data.page) {
						const item = res.data.data.page;
						console.log(item)
						/* if (item.accept == 0) {
							uni.showToast({
								title: '条码未接收',
								icon: 'error'
							});
							return;
						} */
						/* if (item.cwhcode) {
							uni.showToast({
								title: '已入库无法检验',
								icon: 'error'
							});
							return;
						} */
						//this.mocode = item.MoCode
						this.csocode = item.SoCode
						this.modid = item.modid
						this.findPPbarcodeBymodid()
						this.findProd()
						this.findJieshou()
					} else {
						uni.showToast({
							title: '条码不存在',
							icon: 'error'
						});
					}
				});
		},
		h5input(){
			this.getBarcode(this.cbarcode)
		},
		scan() {
			
			let there = this;
			//there.getBarcode('B202203241992');
			uni.scanCode({
				onlyFromCamera: true,
				success: function(res) {
					console.log('条码内容：' + res.result);
					there.getBarcode(res.result);
				},
				fail:function(res){
					uni.showToast({
						title: '识别失败',
						icon: 'error'
					});
				}
			});
		}
	}
};
</script>

<style>
.scroll-Y{
	height: 250upx;
}
.container {
	font-size: 20px;
	background-color: #3884fd;
}
.chr {
	height: 1upx;
	border: none;
	border-top: 1upx solid #d2f1ff;
}
.unigriditem {
	text-align: center;
	text-justify: auto;
	margin: auto auto;
}
.itemth {
	padding: 1upx 10upx;
	font-size: 10rpx;
}
.itembillth {
	margin-top: 10upx;
	font-size: 10upx;
}
.itmemain {
	font-size: 15rpx;
	color: #2480a7;
}
.topborder {
	margin: 2rpx 20rpx;
}
.inputborder {
	background-color: #ffffff;
	border-color: #d2f1ff;
	background-color: #d2f1ff;
	padding: 3rpx 10rpx;
	border-width: 0.5rpx;
	border-style: solid;
	border-radius: 8rpx;
}

.rowborder {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	margin: 3rpx;
}
.billflex {
	display: flex;
	flex-direction: row;
	margin-right: 30rpx;
}
.navborder {
	background-color: #d2f1ff;
	padding: 1rpx;
	margin: 2rpx 10rpx;
	border-color: #eff8ff;
	border-width: 0.5rpx;
	border-style: solid;
	border-radius: 8rpx;
}
.inputbill {
	border-color: #eff8ff;
	border-width: 0.5rpx;
	border-style: solid;
	border-radius: 8rpx;
	width: 230rpx;
	margin-right: 5rpx;
	background-color: #d2f1ff;
}
</style>
